package com.ruixin.base;

import com.ruixin.base.exception.BaseException;
import com.ruixin.base.util.BeanUtil;

import javax.swing.text.html.parser.Parser;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * 基础Annotation处理类
 */
public class BaseParser<T> {

    protected T context;

    protected Class<? extends Annotation> type;

    public BaseParser(T context,Class<? extends Annotation> type) {
        this.context = context;
        this.type = type;
    }

    /**
     * 类注解
     * @param clz
     * @throws BaseException
     */
    public void parseClass(Class<?> clz) throws BaseException{
        isValid(clz);
    }

    /**
     * 属性注解
     * @param bean
     * @param field
     * @throws BaseException
     */
    public void parseField(Object bean, Field field) throws BaseException{
        isValid(field);
    }

    /**
     * 方法注解
     * @param bean
     * @param method
     * @throws BaseException
     */
    public void parseMethod(Object bean, Method method) throws BaseException{
        isValid(method);
    }

    /**
     * 参数注解
     * @param bean
     * @param method
     * @param parameter
     * @throws BaseException
     */
   public void parseParamater(Object bean, Method method,Parameter parameter) throws BaseException{
        isValid(parameter);
   }

   public boolean isValid(Object obj) throws BaseException {
       Annotation annotation = BeanUtil.getAnnotation(obj, this.type);
       if(annotation==null){
           throw new BaseException("对象不包含"+this.type.getTypeName()+"注解！");
       }
        return true;
   }
}
